#include <iostream>
#include <arpa/inet.h>
#include "../tcp/TcpServer.h"
#include "../tcp/http/HttpServer.h"
#include "../tcp/http/HttpResponse.h"
#include "../tcp/http/HttpRequest.h"
#include "../base/log/Logging.h"
#include "../base/log/LogFile.h"


void doGet(const HttpRequest &req, HttpResponse *resp) {
    resp->setStatusCode(HttpResponse::k200Ok);
    resp->addHeader("Content-Type", "text/html");
    resp->setStatusMessage("OK");
    resp->setBody(std::string("<!DOCTYPE html>\n"
                              "<html lang=\"ch\">\n"
                              "<head>\n"
                              "<meta charset=\"UTF-8\">\n"
                              "<title>Get方式访问</title>\n"
                              "</head>\n"
                              "<body>\n"
                              " <div style=\"text-align: center;\">\n"
                              " <h1><a href=\"http://www.bilibili.com\">欢迎来到我的服务器</a></h1>\n"
                              " <h1>200</h1>\n"
                              " </div>\n"
                              "</body>\n"
                              "</html>"));
    resp->setCloseConnection(true);
}

void doPost(const HttpRequest &req, HttpResponse *resp) {
    resp->setStatusCode(HttpResponse::k200Ok);
    resp->addHeader("Content-Type", "text/html");
    resp->setStatusMessage("OK");
    resp->setCloseConnection(true);
}

void doDefault(const HttpRequest &req, HttpResponse *resp) {
    resp->setStatusCode(HttpResponse::k400BadRequest);
    resp->addHeader("Content-Type", "text/html");
    resp->setStatusMessage("Bad Request");
    resp->setBody(std::string("<!DOCTYPE html>\n"
                              "<html lang=\"ch\">\n"
                              "<head>\n"
                              "<meta charset=\"UTF-8\">\n"
                              "<title>出现400问题啦</title>\n"
                              "</head>\n"
                              "<body>\n"
                              " <div style=\"text-align: center;\">\n"
                              " <h1>请求格式有误 / 服务器不支持, 请检查请求格式</h1>\n"
                              " <h1>400</h1>\n"
                              " </div>\n"
                              "</body>\n"
                              "</html>"));
    resp->setCloseConnection(true);
}

void onHttpCallback(const HttpRequest &req, HttpResponse *resp) {
    switch (req.method()) {
        case HttpRequest::kGet :
            doGet(req, resp);
            break;
        case HttpRequest::kPost :
            doPost(req, resp);
            break;
        default:
            doDefault(req, resp);
    }
}

void testHttpServer() {
    InetAddress serverAddr(8200);
    EventLoop loop;
    HttpServer httpServer(&loop, serverAddr, "LinServer");
    httpServer.setThreadNum(10);
    httpServer.setHttpCallback(onHttpCallback);
    httpServer.start();
    loop.loop();
}

std::unique_ptr<LogFile> g_logFile;

void OutputFunc(const char *msg, int len) {
    g_logFile->append(msg, len);
}

void FlushFunc() {
    g_logFile->flush();
}

int main() {

    g_logFile.reset(new LogFile("/home/File/logFile/muduoLog", 1024, true));
    // 设置日志路径和对应的日志文件前缀名

    // Logger::setOutput(OutputFunc);
    // Logger::setFlush(FlushFunc);

    Logger::setLogLevel(Logger::LogLevel::INFO);

    LOG_INFO << "main() start";

    testHttpServer();

    return 0;
}

/*wrk测试性能
 * 12线程 4000并发
 *   Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   247.88ms  460.47ms   1.99s    87.63%
    Req/Sec    53.27     81.36     1.46k    90.75%
  10209 requests in 30.10s, 3.12MB read
  Socket errors: connect 0, read 10261, write 0, timeout 1478
Requests/sec:    339.21
Transfer/sec:    106.12KB
*/